package com.woniu113.junk.user.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu113.junk.user.Param.PageParam;
import com.woniu113.junk.user.exception.UserException;
import com.woniu113.junk.user.model.AdminRole;
import com.woniu113.junk.user.mapper.AdminRoleMapper;
import com.woniu113.junk.user.service.AdminRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 蜡笔没了小新
 * @since 2024年10月16日
 */
@Service
public class AdminRoleServiceImpl extends ServiceImpl<AdminRoleMapper, AdminRole> implements AdminRoleService {
    @Resource
    private AdminRoleMapper adminRoleMapper;
    /*添加角色*/
    @Override
    public void addRole(String role) {
        //先判断是否存在该角色
        List<AdminRole> adminRoles = adminRoleMapper.selectRole();
        for (AdminRole adminRole : adminRoles) {
            if (adminRole.getRoleType().equals(role))throw new UserException("该角色已存在",2001);
        }
        //添加到数据库
        AdminRole adminRole = new AdminRole();
        adminRole.setRoleType(role);
        save(adminRole);
    }

    @Override
    /*
     * 分页查询角色*/
    public Page<AdminRole> selectRole(PageParam param) {
        Page<AdminRole> list =new Page<>(param.getPageNum(), param.getPageSize());
        List<AdminRole> adminRoles = adminRoleMapper.selectRole();
        list.setRecords(adminRoles);

        return list;
    }

    @Override
    public void deleteRole(Integer id) {
        adminRoleMapper.deleteRole(id);
    }
}
